package handlers

import (
	"ecom-manager/cache"
	"ecom-manager/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func ShopAdd(c *gin.Context) {
	shopcode := c.PostForm("shopcode")
	shopname := c.PostForm("shopname")
	shopprice := c.PostForm("shopprice")

	data := models.Shop{
		ShopCode:  shopcode,
		ShopName:  shopname,
		ShopPrice: shopprice,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func ShopOrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_name := c.PostForm("order_name")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	sid, _ := strconv.Atoi(c.PostForm("sid"))

	data := models.Order{
		OrderNo:    order_no,
		OrderName:  order_name,
		OrderPrice: order_price,
		Sid:        sid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))

	Offset := (page - 1) * size

	/*
			SELECT
		  shops.shop_code AS ShopCode,
		  shops.shop_name AS ShopName,
		  shops.shop_price AS ShopPrice,
		  orders.order_no AS OrderNo,
		  orders.order_name AS OrderName,
		  orders.order_price AS OrderPrice
		FROM
		  `orders`
		  LEFT JOIN shops ON orders.sid = shops.id
	*/

	var shoplist []models.OrderList

	redis_key := "shop_order_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("orders").
			Select(
				"shops.shop_code AS ShopCode",
				"shops.shop_name AS ShopName",
				"shops.shop_price AS ShopPrice",
				"orders.order_no AS OrderNo",
				"orders.order_name AS OrderName",
				"orders.order_price AS OrderPrice",
				"orders.id AS OrderId").
			Joins("LEFT JOIN shops ON orders.sid = shops.id").
			Offset(Offset).
			Limit(size).
			Debug().
			Find(&shoplist)

		jsonData, _ := json.Marshal(shoplist)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &shoplist)
	}

	var total int64
	models.DB.Model(&models.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  shoplist,
		"total": total,
	})
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")

	/*
			SELECT
		  shops.shop_code AS ShopCode,
		  shops.shop_name AS ShopName,
		  shops.shop_price AS ShopPrice,
		  orders.order_no AS OrderNo,
		  orders.order_name AS OrderName,
		  orders.order_price AS OrderPrice,
		  orders.id as OrderId
		FROM
		  `orders`
		  LEFT JOIN shops ON orders.sid = shops.id
		WHERE
		  orders.id = 5
	*/
	var shoplist models.OrderList

	redis_key := "shop_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("orders").
			Select(
				"shops.shop_code AS ShopCode",
				"shops.shop_name AS ShopName",
				"shops.shop_price AS ShopPrice",
				"orders.order_no AS OrderNo",
				"orders.order_name AS OrderName",
				"orders.order_price AS OrderPrice",
				"orders.id AS OrderId").
			Joins("LEFT JOIN shops ON orders.sid = shops.id").
			Where("orders.id=?", id).
			Debug().
			Find(&shoplist)

		jsonData, _ := json.Marshal(shoplist)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &shoplist)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": shoplist,
	})
}
